<?php

/**
 * Description of x10_StateManager_State
 *
 * @author prometheus
 */
class x10_StateManager_State extends x10_GeneralClass
{
	public $xName = 'Rendszer állapot: ';

	public $name = '';

	public $extraHash = '';

	public $storePath = '';

	/**
	 * @var x10_StateManager_ClassManager
	 */
	public $classes;

	public $classesLoaded = FALSE;

	public function __construct($name, $storePath, $classesLoaded=NULL, $classes=NULL)
	{
		$this->name = $name;
		$this->xName .= $name;
		$this->storePath = $storePath;
		$this->classes = new x10_StateManager_ClassManager();
		$this->classesLoaded = ($classesLoaded === TRUE);

		if (is_array($classes))
		{
			foreach ($classes as $className)
			{
				$this->classes[] = $className;
			}
		}

		$this->createPathIfNotExists();
		$classListFile = $this->getClassListFileName();
		if (file_exists($classListFile) and is_file($classListFile) and is_readable($classListFile))
		{
			$classListJSON = new x10_Util_JSON($classListFile, TRUE);
			$classList = $classListJSON->get();
			foreach ($classList as $className)
			{
				if (!isset($this->classes[$className])) $this->classes[] = $className;
			}
		}
	}

	public function loadClasses()
	{
		if (!x10::dedicate('x10_StateManager')->getConfig()->getConfigKey('enadbled')) return;

		$packedClassesPath = $this->getPackedSourceFileName();
		if (!$this->classesLoaded and file_exists($packedClassesPath) and is_file($packedClassesPath) and is_readable($packedClassesPath) and count($this->classes) > 0)
		{
			require($packedClassesPath);
		}
	}

	public function storeClasses()
	{
		if (!x10::dedicate('x10_StateManager')->getConfig()->getConfigKey('enadbled')) return;
		
		$classList = array();
		foreach ($this->classes as $className=>$provider)
		{
			$classList[$className] = file_get_contents($provider->classPath);
		}
		file_put_contents(
			$this->getClassListFileName(),
			json_encode(array_keys($classList))
		);
		file_put_contents(
			$this->getPackedSourceFileName(),
			'<'.'?'.'php '.preg_replace('!((([^\'"])?\s*?\<\?php)|(\?\>\s*?([^\'"])?))!i', "$3\n$5", implode("\n", array_values($classList))).' ?'.'>'
		);
	}

	protected function createPathIfNotExists()
	{
		if (!x10::dedicate('x10_StateManager')->getConfig()->getConfigKey('enadbled')) return;

		if (!file_exists($this->storePath) or !is_dir($this->storePath))
		{
			@mkdir($this->storePath, 0777, TRUE);
			@chmod($this->storePath, 0777);
			@chmod(dirname($this->storePath), 0777);
			if (!file_exists($this->storePath))
			{
				throw new x10_E_State_CannotCreateStoreFolder($this->name, $this->storePath);
			}
		}
	}

	protected function getClassListFileName()
	{
		if (empty($this->extraHash))
		{
			return $this->storePath.'classNames.json';
		}
		else
		{
			return $this->storePath.'classNames.'.$this->extraHash.'.json';
		}
	}

	protected function getPackedSourceFileName()
	{
		if (empty($this->extraHash))
		{
			return $this->storePath.'packedSources.php';
		}
		else
		{
			return $this->storePath.'packedSources.'.$this->extraHash.'.php';
		}
	}
}

?>